
/*
 * Copyright (c)  16-11-2 下午6:20
 * Author: oumin
 */

package com.avic.rong.publishVideo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.publishVideo.ProgressWheel;
import com.avic.rong.publishVideo.Record.RecordVideoControl;
import com.avic.rong.publishVideo.Record.SizeSurfaceView;


import java.io.File;


public class RecordVideoActivity extends Activity implements RecordVideoControl.CallBack {


    private ProgressWheel userPhotoLoadProgress;
    private TextView btnLeft;
    private ImageView btnSwitch;
    private SizeSurfaceView surface;
    RecordVideoControl mRecordControl;
    private Activity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_video);
        mActivity = this;
        surface = (SizeSurfaceView) findViewById(R.id.recrod_view);
        userPhotoLoadProgress = (ProgressWheel) findViewById(R.id.user_photo_load_progress);
        btnLeft = (TextView) findViewById(R.id.tv_left);
        btnLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        userPhotoLoadProgress.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mRecordControl != null) {
                    mRecordControl.startRecording();
                }
                return false;
            }
        });
        userPhotoLoadProgress.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_POINTER_UP:
                        if (mRecordControl != null) {
                            mRecordControl.stopRecording(true);
                        }
                        break;
                }
                return false;
            }
        });
        userPhotoLoadProgress.setProgress(0, false);
        btnSwitch = (ImageView) findViewById(R.id.btn_right);
//        String path = LocalStorageUtil.getInstance(mActivity).getUploadVideoPath();
        String path = Environment.getExternalStorageDirectory() +  File.separator +"video_temp.mp4";
        File file = new File(path);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            FileProvider.getUriForFile(RecordVideoActivity.this, "com.avic.rong.provider", file);
        }
        mRecordControl = new RecordVideoControl(mActivity, path, surface, this);
        mRecordControl.ChangeCamera(btnSwitch);

    }


    @Override
    public void RecordingFinish(String path) {
       /* MyFile myFile = new MyFile(path, UploadFile.TYPE_VIDEO);
        addSelectedFileToLayout(myFile);*/
        Intent intent = new Intent();
        intent.putExtra("video_path",path);
        setResult(RESULT_OK,intent);
        finish();
//        if (type == 1) {//个人相册上传视频
//            Intent intent = new Intent();
//            intent.putExtra("video", path);
//            setResult(RESULT_OK, intent);
//            finish();
//        } else {
//            Intent intent = new Intent();
//            intent.setClass(mActivity, PublishDynamicCommentActivity.class);
//            intent.putExtra(PublishDynamicCommentActivity.TYPE, PublishDynamicCommentActivity.TYPE_VIDEO);
//            intent.putExtra(PublishDynamicCommentActivity.VIDEOURL, path);
//            startActivity(intent);
//        }


    }

    @Override
    public void UpdateOfTime(int precent) {
        if (userPhotoLoadProgress != null) {
            float progrss = precent * 3.6f;
            if (precent < 0) {
                progrss = 0;
            }
            if (precent >= 100) {
                progrss = 360;
            }
            userPhotoLoadProgress.setProgress((int) progrss, false);
        }
    }

}
